package cn.bandeqing.frame.exception;

import cn.bandeqing.base.resp.Resp;
import cn.bandeqing.frame.exception.CodeException;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.exception.ExceptionUtil;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public Resp<Void> handleException(Exception e) {
        log.error("GlobalExceptionHandler handleException", e);
        return Resp.error(ExceptionUtil.getMessage(e));
    }

    @ExceptionHandler(value = NoHandlerFoundException.class)
    public Resp<Void> handleNoHandlerFoundException(NoHandlerFoundException e) {
        log.error("GlobalExceptionHandler handleNoHandlerFoundException", e);
        return Resp.error(e.getStatusCode().value(), ExceptionUtil.getMessage(e));
    }

    @ExceptionHandler(value = CodeException.class)
    public Resp<Void> handleCodeException(CodeException ce) {
        log.error("GlobalExceptionHandler handleCodeException : {}", ce.getMessage());
        return Resp.error(ce.getMessage());
    }

}
